package com.java.datastructures.algorithm.recursion;

/**
 * 有n步台阶，一次只能上1步或2步，共有多少种走法？
 * 使用迭代
 *
 */
public class TestStep2 {
    public static void main(String[] args) {
        int loop = loop(15);
        System.out.println("loop = " + loop);
    }

    public static int loop(int n) {
        if (n == 1 || n == 2) {
            return n;
        }
        int one = 2;//初始化为走到第二级台阶的走法
        int two = 1;//初始化为走到第一级台阶的走法
        int sum = 0;

        for (int i = 3; i <= n; i++) {
            //最后跨两步 + 最后跨一步
            sum = two + one;
            two = one;
            one = sum;
        }
        return sum;
    }

}
